﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

using System.Collections;
using System.Collections.ObjectModel;
using NamTrungBS.DataLayer;
namespace NamTrungBS
{
    public partial class MainForm : Office2007RibbonForm
    {
        private string _tenhoadon;
        private string _tennguoimua;
        private double _tongtienmua= 0;
        private double _tienno = 0;
        private double _tongtien;
        private ChiTietHoaDon itemChitiet;
        private HoaDon hoadonnew;
        bool flagOpenOrder = false;
        int _sothutu = 1;
        public MainForm()
        {
            InitializeComponent();
            this.txtKhachHang.AutoCompleteCustomSource.AddRange(KhachHangDao.GetAllCustomer().ToArray()); 
            //gan gia tri cho cac fiel trong table hoadon
            _tennguoimua = this.txtKhachHang.Text;
            
            if (flagOpenOrder == false)
            {
                _tenhoadon = _tennguoimua + DateTime.Now.ToLongTimeString();
                hoadonnew = HoaDonDao.CreatNewHoaDon(_tenhoadon, _tennguoimua, _tongtienmua, _tienno);
            }
            FillDataSanPham();//Do du lieu vao gridview san pham
            this.txtSearch.AutoCompleteCustomSource.AddRange(SanPhamDao.GetAllProduct().ToArray());
            
        }
        private void FillDataSanPham()
        {
            if (txtSearch.Text == null)
            {
                this.dataGridViewSP.DataSource = SanPhamDao.GetAllSanPhamByDG(Int32.Parse(cboCapKhachHang.Text));
              
            }
            else
            {
                this.dataGridViewSP.DataSource = SanPhamDao.GetSanPhamByName(txtSearch.Text, Int32.Parse(cboCapKhachHang.Text));
            }
            
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            FillDataSanPham();
        }
        
        private void btnBanhang_Click(object sender, EventArgs e)
        {
            this.ribbonClientPanel1.Visible = true;
            this.dataGridViewSP.Columns[0].Visible = false;
            this.dataGridViewSP.Columns[1].Width = 250;
           
        }

        private void cboCapKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDataSanPham();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                this.dataGridViewSP.Focus();
            }
            else if (e.KeyValue==13)
            {
                this.dataGridViewSP.Focus();
            }
            
        }

        private void dataGridViewSP_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            if (e.KeyChar == 32)
            {
                this.txtSoluong.Focus();
                this.txtSoluong.Clear();
            }
            else
            {
                MessageBox.Show("Vui lòng nhấn Space để chọn sản phẩm",
                    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtSoluong_KeyPress(object sender, KeyPressEventArgs e)
        {
            double soluongmua=0;
            double dongia=0;
            if (e.KeyChar == 13)
            {
                DataGridViewRow rowSanPham = dataGridViewSP.SelectedRows[0];
                soluongmua=Double.Parse(txtSoluong.Text);
                dongia=Double.Parse(rowSanPham.Cells[5].Value.ToString());
                itemChitiet = new ChiTietHoaDon
                {
                    STT = _sothutu,
                    MaHD = hoadonnew.MaHD,
                    MaSP = Int32.Parse(rowSanPham.Cells[0].Value.ToString()),
                    SoLuong = soluongmua,
                    TenSP=rowSanPham.Cells[1].Value.ToString(),
                    TonDau = Double.Parse(rowSanPham.Cells[3].Value.ToString()),
                    DVT = rowSanPham.Cells[2].Value.ToString(),
                    DonGia = dongia,
                    ThanhTien=dongia*soluongmua
                };
                ChiTietHoaDonDao.NewHoaDonChiTiet(itemChitiet);
                _sothutu++;
                this.txtSearch.Clear();
                this.txtSearch.Focus();
                FillGridViewHoaDon();
                
            }
        }
        /// <summary>
        /// Đổ dữ liệu vào gridview hoa don
        /// </summary>
        private void FillGridViewHoaDon()
        {
            try {
                _tongtienmua = ChiTietHoaDonDao.GetSumMoney(hoadonnew.MaHD);
                this.txtThanhTien.Text = _tongtienmua.ToString();
                dataGridViewHoaDon.DataSource = ChiTietHoaDonDao.GetChiTietHdByID(hoadonnew.MaHD);
                dataGridViewHoaDon.Columns[0].Visible = false;
                dataGridViewHoaDon.Columns[1].Visible = false;
                dataGridViewHoaDon.Columns[2].Visible = false;
                dataGridViewHoaDon.Columns[9].Visible = false;
                dataGridViewHoaDon.Columns[3].Width = 30;
                dataGridViewHoaDon.Columns[4].Width = 200;
                dataGridViewHoaDon.FirstDisplayedScrollingRowIndex = dataGridViewHoaDon.Rows.Count - 1;
            }
            catch (Exception ex)
            {
               
                return;
            }
            
        }
        private void btnThemHangMoi_Click(object sender, EventArgs e)
        {
           ThemMoi formThemMoi=new ThemMoi();
           formThemMoi.ShowDialog();
          
           if (formThemMoi.IsDisposed)
           {
               itemChitiet = formThemMoi.chitiet;
               itemChitiet.MaHD = hoadonnew.MaHD;
               itemChitiet.STT = _sothutu;
               ChiTietHoaDonDao.NewHoaDonChiTiet(itemChitiet);
               formThemMoi.Close();
               FillGridViewHoaDon();
               _sothutu++;
           }
        }
       

        private void btnInHoaDon_Click(object sender, EventArgs e)
        {
            HoaDonDao.UpdateHoaDon(hoadonnew.MaHD, _tenhoadon, _tennguoimua, _tongtien, _tienno);
            HoaDonShow frmHoadon = new HoaDonShow();
            frmHoadon.MaHD = hoadonnew.MaHD;
            frmHoadon.Tenkh = _tennguoimua;
            frmHoadon.Show();
        }

        private void txtTienNo_TextChanged(object sender, EventArgs e)
        {
            try {
                if (Double.TryParse(txtTienNo.Text, out _tienno))
                {
                    _tongtien = _tongtienmua + _tienno;
                    txtTongTien.Text = _tongtien.ToString();
                }
                else
                    MessageBox.Show("Giá trị nhập không đúng !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.ribbonClientPanel1.Visible = false;
        }

        private void dataGridViewHoaDon_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int index=e.RowIndex;
            DataGridViewRow rowChitiet = dataGridViewHoaDon.Rows[index];
            int machitiet=0;
            machitiet=int.Parse(rowChitiet.Cells[0].Value.ToString());
            ChiTietHoaDon item = ChiTietHoaDonDao.ChiTietHoaDonItem(machitiet);
            item.SoLuong = double.Parse(rowChitiet.Cells["SoLuong"].Value.ToString());
            item.DonGia = double.Parse(rowChitiet.Cells["DonGia"].Value.ToString());
            item.ThanhTien = item.SoLuong * item.DonGia;
            ChiTietHoaDonDao.UpdateHoaDonChiTiet(item);
            FillGridViewHoaDon();
        }

        private void btnThongke_Click(object sender, EventArgs e)
        {
            ThongKe frmThongke = new ThongKe();
            frmThongke.ShowDialog();
        }

        private void txtKhachHang_TextChanged(object sender, EventArgs e)
        {
            _tennguoimua = this.txtKhachHang.Text;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            LuuHoaDon hoadonSave = new LuuHoaDon { 
             MaHD=hoadonnew.MaHD,
             TenHoaDon=this.txtKhachHang.Text+DateTime.Now.ToLongTimeString(),
             MoTa=DateTime.Now.ToLongTimeString()
            };
            LuuHoaDonForm SaveFrm = new LuuHoaDonForm(hoadonSave);
            SaveFrm.ShowDialog();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            MoHoaDon frmOpenOrder = new MoHoaDon();
            frmOpenOrder.ShowDialog();
            if (frmOpenOrder.IsDisposed)
            {
                _tenhoadon = frmOpenOrder.hoaDonOpen.TenHoaDon;
                hoadonnew = HoaDonDao.GetListHoaDonByID(frmOpenOrder.hoaDonOpen.MaHD);
                flagOpenOrder = true;
                FillGridViewHoaDon();
                _sothutu = dataGridViewHoaDon.Rows.Count+1;
            }
            this.ribbonClientPanel1.Visible = true;
            this.dataGridViewSP.Columns[0].Visible = false;
            this.dataGridViewSP.Columns[1].Width = 250;
        }
        /// <summary>
        /// Them 1 san pham vao grid hoa don
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewHoaDon_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            machitiet = int.Parse(rowChitiet.Cells[0].Value.ToString());
            ChiTietHoaDon item = ChiTietHoaDonDao.ChiTietHoaDonItem(machitiet);
            item.SoLuong = double.Parse(rowChitiet.Cells["SoLuong"].Value.ToString());
            item.DonGia = double.Parse(rowChitiet.Cells["DonGia"].Value.ToString());
            item.ThanhTien = item.SoLuong * item.DonGia;
            ChiTietHoaDon itemAdd = new ChiTietHoaDon { 
                STT=_sothutu,
                MaHD=hoadonnew.MaHD,
                MaSP=0,

            };
            itemChitiet = formThemMoi.chitiet;
            itemChitiet.MaHD = hoadonnew.MaHD;
            itemChitiet.STT = _sothutu;
            ChiTietHoaDonDao.NewHoaDonChiTiet(itemChitiet);
            formThemMoi.Close();
            FillGridViewHoaDon();
            _sothutu++;
        }
        
    }
}
